package com.wkbb.basic.controller.manage;

import com.alibaba.fastjson.JSONArray;
import com.wkbb.basic.model.OrgStructure;
import com.wkbb.basic.service.OrgStructureService;
import com.wkbb.basic.service.OrgStructureUserService;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lizhichao
 * @date 2019/8/9
 * @description
 */
@RestController
@Api(description = "组织架构管理")
@RequestMapping("/basic/{apiVersion}/orgStructure/manage")
@Slf4j
public class OrgStructureController extends BaseController {

    @Autowired
    private OrgStructureService orgStructureService;

    @ApiOperation("初始化组织树形结构")
    @RequestMapping(value = "/initOrgStructureTree", method = RequestMethod.GET)
    public ResultDTO<List<OrgStructure>> initOrgStructureTree(
            @ApiParam(value = "接口版本",required = true,defaultValue = "v1") @PathVariable(value = "apiVersion",required = true) String apiVersion,
            @ApiParam(value = "组织类型",required = true) @RequestParam(name = "type",required = true) String type) {
        log.info("***********OrgStructureController.initOrgStructureTree******apiVersion:{},groupType:{}", apiVersion, type);

        try {

            UserDto userDto = this.getUserByLogin();
            if (userDto == null) {
                return new ResultDTO(ErrorCode.USER_NOT_EXIST);
            }

            Long orgId = ((JSONArray)userDto.getRoleType()).getJSONObject(0).getLong("orgId");
            Map<String, Object> params = new HashMap();
            params.put("orgId", orgId);
            params.put("parentId", 0);
            params.put("type", type);
            List<OrgStructure> groupTreeList = this.orgStructureService.initOrgStructureTreeList(params);
            return new ResultDTO(groupTreeList);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("OrgStructureController.initOrgStructureTree**初始化组织树形结构异常", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation("查询指定组织树形结构")
    @RequestMapping(value = "/getOrgStructureTreeById", method = RequestMethod.GET)
    public ResultDTO<List<OrgStructure>> getOrgStructureTreeById(
            @ApiParam(value = "接口版本",required = true,defaultValue = "v1") @PathVariable(value = "apiVersion",required = true) String apiVersion,
            @ApiParam(value = "上级组织架构Id",required = true) @RequestParam(name = "id",required = true) Long id) {
        log.info("***********OrgStructureController.getOrgStructureTree******apiVersion:{},id:{}", apiVersion, id);

        try {
            UserDto userDto = this.getUserByLogin();
            if (userDto == null) {
                return new ResultDTO(ErrorCode.USER_NOT_EXIST);
            }

            Long orgId = ((JSONArray)userDto.getRoleType()).getJSONObject(0).getLong("orgId");
            Map<String, Object> params = new HashMap();
            params.put("orgId", orgId);
            params.put("id", id);
            List<OrgStructure> groupTreeList = this.orgStructureService.getOrgStructureTreeList(params);
            return new ResultDTO(groupTreeList);

        } catch (Exception e) {
            log.error("OrgStructureController.getOrgStructureTree**初始化组织树形结构异常", e);
            e.printStackTrace();
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation("增加自定义组织架构")
    @RequestMapping(value = "/addOrgStructure", method = RequestMethod.POST)
    public ResultDTO<OrgStructure> addOrgStructure(
            @ApiParam(value = "接口版本",required = true,defaultValue = "v1") @PathVariable(value = "apiVersion",required = true) String apiVersion,
            @ApiParam(value = "上级组织",required = false) @RequestParam(name = "parentId",required = false) Long parentId,
            @ApiParam(value = "组织类型(0:正常1:校友2退休3其他)",required = true) @RequestParam(name = "type",required = true) Integer type,
            @ApiParam(value = "组织名称",required = true) @RequestParam(name = "name",required = true) String name) {
        log.info("***********OrgStructureController.addOrgStructure******apiVersion:{},parentId:{},type:{},name:{}", new Object[]{apiVersion, parentId, type, name});

        try {
            UserDto userDto = this.getUserByLogin();
            if (userDto == null) {
                return new ResultDTO(ErrorCode.USER_NOT_EXIST);
            } else if (StringUtil.isEmpty(name)) {
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }

            Long orgId = ((JSONArray)userDto.getRoleType()).getJSONObject(0).getLong("orgId");
            OrgStructure orgStructure = new OrgStructure();
            orgStructure.setOrgId(orgId);
            orgStructure.setParentId(parentId);
            orgStructure.setName(name);
            orgStructure.setType(type.shortValue());
            orgStructure.setStatus(true);
            orgStructure.setCreateBy(userDto.getName());
            orgStructure.setUpdateBy(userDto.getName());
            this.orgStructureService.insert(orgStructure);

            return new ResultDTO(orgStructure);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("OrgStructureController.addOrgStructure**增加自定义组织异常", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation("修改组织架构")
    @RequestMapping(value = "/updateOrgStructure", method = RequestMethod.PUT)
    public ResultDTO updateOrgStructure(
            @ApiParam(value = "接口版本",required = true,defaultValue = "v1") @PathVariable(value = "apiVersion",required = true) String apiVersion,
            @ApiParam(value = "组织架构ID",required = true) @RequestParam(name = "id",required = true) Long id,
            @ApiParam(value = "组织名称",required = true) @RequestParam(name = "name",required = true) String name) {
        log.info("***********OrgStructureController.updateOrgStructure******apiVersion:{},id:{},groupName:{}", new Object[]{apiVersion, id, name});

        try {
            UserDto userDto = this.getUserByLogin();
            if (userDto == null) {
                return new ResultDTO(ErrorCode.USER_NOT_EXIST);
            }

            Long orgId = ((JSONArray)userDto.getRoleType()).getJSONObject(0).getLong("orgId");
            OrgStructure orgStructure = new OrgStructure();
            orgStructure.setId(id);
            orgStructure.setOrgId(orgId);
            orgStructure.setName(name);
            orgStructure.setUpdateBy(userDto.getName());
            this.orgStructureService.update(orgStructure);
            return new ResultDTO();

        } catch (Exception e) {
            e.printStackTrace();
            log.error("OrgStructureController.updateOrgStructure**修改组织异常", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation("删除组织架构")
    @RequestMapping(value = "/deleteOrgStructure", method = RequestMethod.DELETE)
    public ResultDTO deleteOrgStructure(
            @ApiParam(value = "接口版本",required = true,defaultValue = "v1") @PathVariable(value = "apiVersion",required = true) String apiVersion,
            @ApiParam(value = "组织架构ID",required = true) @RequestParam(name = "id",required = true) Long id) {
        log.info("***********OrgStructureController.deleteOrgStructure******apiVersion:{},id:{}", apiVersion, id);

        try {
            UserDto userDto = this.getUserByLogin();
            if (userDto == null) {
                return new ResultDTO(ErrorCode.USER_NOT_EXIST);
            }

            Long orgId = ((JSONArray)userDto.getRoleType()).getJSONObject(0).getLong("orgId");
            this.orgStructureService.deleteOrgStructureBatch(id, orgId);
            return new ResultDTO();

        } catch (Exception e) {
            e.printStackTrace();
            log.error("OrgStructureController.deleteOrgStructure**删除组织异常", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
}
